# coding=utf-8
import os;
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "auto_sale_spider.settings")  # NoQA
import django;
django.setup()  # NoQA

from django.shortcuts import render
from django.http import HttpResponse
from auto_sale.models import *


# Create your views here.

def base(request):
    return render(request, 'auto_sale/base.html')


def single(request):
    product_id = request.GET.get('product_id')
    try:
        product = Product.objects.get(id=product_id)
    except:
        return HttpResponse('product is not exist', content_type='text')

    if not (product.is_valid == True and product.is_deleted==False):
        res = not(product.is_valid == True & product.is_deleted==False)
        return HttpResponse('product is unpublished', content_type='text')

    size_list = Size.objects.filter(product=product, is_valid=True)
    color_list = Color.objects.filter(product=product, is_valid=True)
    booth_image_list = BoothImage.objects.filter(product=product, is_valid=True)
    detail_image_list = DetailImage.objects.filter(product=product, is_valid=True)



    return render(request, 'auto_sale/single.html',
                  {'product': product, 'size_list': size_list, 'color_list': color_list,
                   'booth_image_list': booth_image_list, 'detail_image_list': detail_image_list})


# def fun():
#     product = Product.objects.using('auto_sale_db').get(id=1)
#     if not (product.is_valid == True & product.is_deleted==False):
#         res = not(product.is_valid == True & product.is_deleted==False)
#         print 'product is unpublished '+ str(product.is_valid) + ' ' + str(product.is_deleted) + ' ' + str(res)
#
# if __name__ == '__main__':
#     fun()